package org.ltlwill.tess;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.Tesseract1;
import net.sourceforge.tess4j.util.LoadLibs;

import java.io.File;

public class OCRDemo {
    public static void main(String args[]) throws Exception {
        ITesseract instance = new Tesseract1();
        synchronized (instance){
           // instance.setDatapath("/Users/lizhihe/secoo_work/campus/sc-tess4j/src/main/java/resources/testdatas"); //相对目录，这个时候tessdata目录和src目录平级
            instance.setDatapath(LoadLibs.extractTessResources("testdatas").getParent());
//        instance.setDatapath("E:\\myProgram\\java\\ocrdemo\\tessdata");//支持绝对目录
            instance.setLanguage("chi_sim");//选择字库文件（只需要文件名，不需要后缀名）



            try {

                String img = "/Users/lizhihe/Pictures/WechatIMG14.jpeg";
                File imageFile = new File(img);
                String result = instance.doOCR(imageFile);//开始识别
                System.out.println(result);//打印图片内容
            } catch (Exception e) {
                e.printStackTrace();
                //  System.out.println(e.toString());//打印图片内容
            }
        }


    }

}